package com.oto.web.resource;

import java.util.Collection;

import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.oto.Constants;
import com.oto.Status;
import com.oto.model.AccountDO;
import com.oto.model.FlowDO;
import com.oto.model.PackDO;
import com.sun.jersey.api.view.Viewable;

/**
 * @author xueqiang.mi
 * @since 2010-12-22
 */
@Path("/home")
@Component
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class HomeResource extends BaseResource {

	private long packId = 0;

	@GET
	@Path("{packId}")
	public Response byPack(@PathParam("packId") long packId) {
		this.packId = packId;
		return Response.ok(new Viewable("index", this)).build();
	}

	@POST
	@Path("ajax/crtPack")
	@Consumes("application/x-www-form-urlencoded")
	@Produces("text/plain")
	public String crtPack(@FormParam("name") String name) {
		PackDO p = new PackDO();
		p.setName(name);
		p.setOwner(getUserName());
		packService.add(p);
		return Constants.STATUS_DONE;
	}

	@GET
	@Path("ajax/delPhone")
	@Produces("text/plain")
	public String delFlow(@QueryParam("id") long id) {
		FlowDO f = flowService.get(id);
		if (f == null) {
			return Constants.STATUS_ERROR + " 没有查询到id=[" + id + "]的电话!";
		}
		if (f.getOwner().equals(getUserName())) {
			flowService.del(id);
		}
		return Constants.STATUS_DONE;
	}

	@GET
	@Path("ajax/delPack")
	@Produces("text/plain")
	public String delPack(@QueryParam("id") long id) {
		PackDO p = packService.get(id);
		if (p == null) {
			return Constants.STATUS_ERROR + " 没有查询到id=[" + id + "]的电话分组!";
		}
		if (p.getOwner().equals(getUserName())) {
			packService.delAndRemoveFlowsInIt(getUserName(), id);
		}
		return Constants.STATUS_DONE;
	}

	public Collection<FlowDO> getFlows() {
		if (getUserName() == null) {
			return null;
		}
		if (packId != 0) {
			return flowService.getByPack(packId);
		}
		return flowService.getLattestByUser(getUserName(), Constants.PAGE_SIZE);
	}

	public Collection<String> getFrids() {
		if (getUserName() == null) {
			return null;
		}
		AccountDO account = accountService.get(getUserName());
		return account.getFriends();
	}

	public Collection<PackDO> getPacks() {
		if (getUserName() == null) {
			return null;
		}
		return packService.getByUser(getUserName());
	}

	@GET
	@Path("ajax/setStatus")
	@Produces("text/plain")
	public String setStatus(@QueryParam("id") long id) {
		FlowDO f = flowService.get(id);
		if (f == null) {
			return Constants.STATUS_ERROR + " 没有查询到id=[" + id + "]的电话!";
		}
		if (f.getOwner().equals(getUserName())) {
			flowService.setStatus(id, Status.FOR_MOBILE);
		}
		return Constants.STATUS_DONE;
	}
}
